from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.

from django.contrib.auth.backends import ModelBackend
from app.models import User


# 重写校验成功的返回
def jwt_response_payload_handler(token, user=None, request=None):
    """
    设置jwt登陆返回的格式
    :param token:
    :param user:
    :param request:
    :return:
    """
    return JsonResponse({
        "errornumber": "0",
        'token': token,
    }
    )


# 重写登录校验方式
class UserAuthBackend(ModelBackend):
    # def authenticate(self, request, username=None, password=None, **kwargs):
    def authenticate(self, request, username=None, password=None, **kwargs):
        # 用username实际传进来的是email

        if User.objects.filter(email=username).exists():
            user = User.objects.get(email=username)
            print(password)
            if user.check_password(password):
                return user
            else:
                return None
        else:
            return None


